-- dream_dust
-- create by zhuangsl
-- 怪物主动技CD增加

return {
    apply = function(prop, baseValue)
        return prop[2];
    end,

    record = function(who, prop, path)
        if who.type ~= OBJECT_TYPE_MONSTER then
            return false;
        end

        local props = PropM.fetchProps(who, prop[2]);
        if #props <= 0 then
            return false;
        end

        -- 重置出现回合
        local p = props[1];
        local grid = DungeonM.getGridByPos(who:getPos());
        local openRound = grid.openRound;
        local round = CombatM.getRound();
        local cd = tonumber(p[2]);
        local re = (round - openRound) % cd;

        if re == 0 then
            openRound = round - cd;
        else
            openRound = round - re;
        end
        grid.openRound = openRound;

        -- 用一个cd更长的技能替换掉
        p[2] = cd + prop[3];
        PropM.deletePropById(who, "skill", p[1]);
        PropM.record(who, p, "skill");

        return false;
    end,

    clear = function(who, prop)
        if who.type ~= OBJECT_TYPE_MONSTER and who.type ~= OBJECT_TYPE_GHOST_SHIP then
            return;
        end

        local props = PropM.fetchProps(who, prop[2]);
        if #props <= 0 then
            local round = CombatM.getRound();
            if who.type == OBJECT_TYPE_GHOST_SHIP then
                -- 幽灵船，重置攻击回合
                who.action = round;
            end

            if who:isSlime() or ActivityBossM.isSpecialBoss(who) then
                -- 冈布奥怪，重置攻击回合
                who.skill_round = round;
            end

            return;
        end

        -- 重置出现回合
        local p = props[1];
        local grid = DungeonM.getGridByPos(who:getPos());
        local openRound = grid.openRound;
        local round = CombatM.getRound();
        local cd = tonumber(p[2]);

        openRound = openRound + prop[3] + round - prop[4];
        grid.openRound = openRound;

        -- 用一个cd较短的技能替换掉
        p[2] = cd - prop[3];
        PropM.deletePropById(who, "skill", p[1]);
        PropM.record(who, p, "skill");
    end,
};
